﻿using Sidvall.WorkItemManager.Data;
using System.ComponentModel;

namespace Sidvall.WorkItemManager.Workflow.WorkItems
{
    public class ConsoleWorkItemContext : INotifyPropertyChanged
    {
        #region Public Members

        public event PropertyChangedEventHandler PropertyChanged;
        public event System.EventHandler<Sidvall.Business.ChildChangedEventArgs> ChildChanged;

        #region Arguments

        private string _Arguments = string.Empty;
        public string Arguments
        {
            get
            {
                return _Arguments;
            }
            set
            {
                value = Sidvall.Business.ValidationManager.GetValidString(value, false, null);
                if (_Arguments == value)
                    return;
                _Arguments = value;
                OnPropertyChanged(nameof(Arguments));
            }
        }

        #endregion
        #region FileName

        private string _FileName = string.Empty;
        public string FileName
        {
            get
            {
                return _FileName;
            }
            set
            {
                value = Sidvall.Business.ValidationManager.GetValidString(value, false, null);
                if (_FileName == value)
                    return;
                _FileName = value;
                OnPropertyChanged(nameof(FileName));
            }
        }

        #endregion
        #region WorkingDirectory

        private string _WorkingDirectory = string.Empty;
        public string WorkingDirectory
        {
            get
            {
                return _WorkingDirectory;
            }
            set
            {
                value = Sidvall.Business.ValidationManager.GetValidString(value, false, null);
                if (_WorkingDirectory == value)
                    return;
                _WorkingDirectory = value;
                OnPropertyChanged(nameof(WorkingDirectory));
            }
        }

        #endregion
        #region CreateNoWindow

        private bool _CreateNoWindow;
        public bool CreateNoWindow
        {
            get
            {
                return _CreateNoWindow;
            }
            set
            {
                if (_CreateNoWindow == value)
                    return;
                _CreateNoWindow = value;
                OnPropertyChanged(nameof(CreateNoWindow));
            }
        }

        #endregion
        #region ErrorDialog

        private bool _ErrorDialog;
        public bool ErrorDialog
        {
            get
            {
                return _ErrorDialog;
            }
            set
            {
                if (_ErrorDialog == value)
                    return;
                _ErrorDialog = value;
                OnPropertyChanged(nameof(ErrorDialog));
            }
        }

        #endregion
        #region UseShellExecute

        private bool _UseShellExecute = true;
        public bool UseShellExecute
        {
            get
            {
                return _UseShellExecute;
            }
            set
            {
                if (_UseShellExecute == value)
                    return;
                _UseShellExecute = value;
                OnPropertyChanged(nameof(UseShellExecute));
            }
        }

        #endregion
        #region WindowStyle

        private Sidvall.SystemEnvironment.ViewSizePreference _WindowStyle = SystemEnvironment.ViewSizePreference.Default;
        public Sidvall.SystemEnvironment.ViewSizePreference WindowStyle
        {
            get
            {
                return _WindowStyle;
            }
            set
            {
                if (_WindowStyle == value)
                    return;
                _WindowStyle = value;
                OnPropertyChanged(nameof(WindowStyle));
            }
        }

        #endregion
        #region SensorProviderCodename

        private string _SensorProviderCodename = string.Empty;
        public string SensorProviderCodename
        {
            get
            {
                return _SensorProviderCodename;
            }
            set
            {
                value = Sidvall.Business.ValidationManager.GetValidString(value, false, null);
                if (_SensorProviderCodename == value)
                    return;
                _SensorProviderCodename = value;
                OnPropertyChanged(nameof(SensorProviderCodename));
            }
        }

        #endregion
        #region Sensors

        private SensorCollection _Sensors;
        public SensorCollection Sensors
        {
            get
            {
                if (_Sensors == null)
                    this.Sensors = new SensorCollection();
                return _Sensors;
            }
            private set
            {
                if (_Sensors != null)
                {
                    _Sensors.ChildChanged -= OnChildChanged;
                    _Sensors.CollectionChanged -= OnChildChanged;
                }
                _Sensors = value;
                if (_Sensors != null)
                {
                    _Sensors.ChildChanged += OnChildChanged;
                    _Sensors.CollectionChanged += OnChildChanged;
                }
                OnPropertyChanged(nameof(Sensors));
            }
        }


        #endregion

        #region OnChildChanged

        protected void OnChildChanged(object sender, Sidvall.Business.ChildChangedEventArgs e)
        {
            OnChildChanged(e);
        }
        protected void OnChildChanged(object child, System.Collections.Specialized.NotifyCollectionChangedEventArgs childArgs)
        {
            var args = new Sidvall.Business.ChildChangedEventArgs(child, childArgs);
            OnChildChanged(args);
        }
        protected virtual void OnChildChanged(Sidvall.Business.ChildChangedEventArgs args)
        {
            if (this.ChildChanged != null)
                this.ChildChanged.Invoke(this, args);
        }

        #endregion
        #region OnPropertyChanged

        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged.Invoke(this, args);
        }

        #endregion
        #region Deserialize

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public static ConsoleWorkItemContext Deserialize(string value)
        {
            ConsoleWorkItemContext item;

            item = null;
            try
            {
                if (!string.IsNullOrWhiteSpace(value))
                    item = Sidvall.Serialization.SerializerManager.Current.Deserialize<ConsoleWorkItemContext>(value);
            }
            catch
            {
                item = null;
            }
            if (item == null)
                item = new ConsoleWorkItemContext();
            return item;
        }

        #endregion
        #region Serialize

        public static string Serialize(ConsoleWorkItemContext item)
        {
            return Sidvall.Serialization.SerializerManager.Current.SerializeObject(item);
        }

        #endregion

        #endregion
    }
}
